//
//  ZyaClientAddressDetailController.swift
//  SlaughterClient
//
//  Created by zwb on 2018/5/31.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

class ZyaClientAddressDetailController: UIViewController {

    var currentMark = 0
    var newAddClientHandler: ((String) -> Void)?
    var idKey = ""
    var isSelect = false {
        didSet {
            if isSelect {
                selectButton.setTitle("默认地址", for: UIControl.State())
                selectButton.setTitleColor(Theme.orange, for: UIControl.State())
                selectButton.setImage(#imageLiteral(resourceName: "s_select"), for: UIControl.State())
                currentMark = 1
            } else {
                selectButton.setTitle("设为默认", for: UIControl.State())
                selectButton.setTitleColor(UIColor.black, for: UIControl.State())
                selectButton.setImage(#imageLiteral(resourceName: "s_unselect"), for: UIControl.State())
                currentMark = 0
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        edgesForExtendedLayout = UIRectEdge()
        view.backgroundColor = UIColor.white
        view.addSubview(addressTf)
        view.addSubview(selectButton)
        view.addSubview(submitButton)
        
        selectButton.addTarget(self, action: #selector(selectAction), for: .touchUpInside)
        submitButton.addTarget(self, action: #selector(submitAction), for: .touchUpInside)
        
        addressTf.snp.makeConstraints { (maker) in
            maker.leading.equalTo(8)
            maker.trailing.equalToSuperview().inset(8)
            maker.top.equalTo(12)
            maker.height.equalTo(90)
        }
        
        selectButton.snp.makeConstraints { (maker) in
            maker.leading.equalTo(12)
            maker.width.equalTo(90)
            maker.top.equalTo(addressTf.snp.bottom).offset(12)
        }
        
        submitButton.snp.makeConstraints { (maker) in
            maker.leading.equalTo(8)
            maker.trailing.equalToSuperview().inset(8)
            maker.top.equalTo(selectButton.snp.bottom).offset(24)
            maker.height.equalTo(44)
        }
    }
    
    @objc private func selectAction() {
        isSelect = !isSelect
    }
    
    @objc private func submitAction() {
        
        view.endEditing(true)
        
        let data = ZyaClientAddress(JSON: ["client_add" : ""])
        
        data?.clientAdd = addressTf.text
        data?.currMark = currentMark
        data?.idKey = idKey
        data?.clientId = USERINFO.kfId!
        data?.clientName = USERINFO.kfName!
        
        guard let json = data?.toJSONString() else { return }
        
        AWLog(json)
        
        KSNetwork.shared.doRequest(API.addClientAddress(data: json, idKey: idKey)) { (data: BaseResult<OperateResult>) in
            if data.message.unwrap.contains("成功") {
                if let hander = self.newAddClientHandler {
                    hander(self.addressTf.text)
                    self.navigationController?.popViewController(animated: true)
                } else {
                    let vc = self.navigationController?.viewControllers[1] as? ZyaClientAddressController
                    vc?.fetchData()
                    self.navigationController?.popToViewController(vc!, animated: true)
                }
            }
        }
    }
    
    lazy var addressTf: PlaceHolderTextView = {
        let textView = PlaceHolderTextView()
        textView.placeHolder = "请填写地址"
        textView.layer.borderWidth = 1
        textView.layer.borderColor = UIColor.lightGray.cgColor
        textView.layer.cornerRadius = 4
        textView.backgroundColor = UIColor.white
        textView.font = UIFont.systemFont(ofSize: 16)
        return textView
    }()
    
    lazy var selectButton: UIButton = {
        let button = UIButton()
        button.setTitle("设为默认", for: UIControl.State())
        button.setTitleColor(UIColor.black, for: UIControl.State())
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.setImage(#imageLiteral(resourceName: "s_unselect"), for: UIControl.State())
        return button
    }()
    
    lazy var submitButton: UIButton = {
        let button = UIButton()
        button.layer.cornerRadius = 5
        button.backgroundColor = Theme.primaryColor()
        button.setTitle("保存", for: UIControl.State())
        return button
    }()
}
